<?php
namespace Home\Controller;
use Think\Controller;
class SystemController extends CommunalController{
	//个人资料 designed by:zhangsen 2017-08-09
	function personal_data(){
		//用户详情
		$user=$this->get_user($this->login_id);
		$center=$this->get_oa_dep_info($user['center_id']);
		$user['center']=$center['DEPT_DESCR'];
		$department=$this->get_oa_dep_info($user['department_id']);
		$user['deps']=$department['HPS_DEPT_FULLDESCR'];
		$this->assign('user',$user);
		//编辑
		if($_REQUEST['action']=='edit')
		{
			$db_user=M('db_user');
			if(!empty($_REQUEST['regname'])){
				//判断用户名是否重复
				$where_regname['regname']=$_REQUEST['regname'];
				$regname_list=$db_user->where($where_regname)->find();
				if(!empty($regname_list) && ($regname_list['id']!=$this->login_id))
				{
					$this->error("该登录用户名已存在！");die;
				}
				$where_edit['regname']=$_REQUEST['regname'];
			}
			$where_edit['id']=$this->login_id;
			$where_edit['password']=$_REQUEST['password'];
			$db_user->save($where_edit);
			if($user['password']!=$_REQUEST['password']){
				session_destroy();
			}
			$this->success("操作成功",__APP__."/System/personal_data");die;
		}
		$this->display();
	}
	
	//解除微信绑定操作
	function personal_ajax(){
		$db_user=M ('db_user');
		if($_REQUEST['action']=='delete')
		{
			//完结
			$where['id']=$this->login_id;
			$where['openid']=NULL;
			$result_bool=$db_user->save($where);
		}
		if($result_bool){
			$data['status']=self::YES;
		}
		echo json_encode($data);die;
	}

	//公告 designed by:zhangsen 2017-08-30
	function notice_list(){
		//赋值
		if(!empty($_REQUEST['tid'])){$tid=$_REQUEST['tid'];}else{$tid=-10;}
		$this->assign('tid',$tid);
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		//列表
		import("ORG.Util.Page");
		if(get_permission('GG',1)){
		}elseif(get_permission('GG',3)){
			$where['_string']="find_in_set('".$this->login_id."',uids) or (is_all=1) or add_uid='".$this->login_id."'";
		}else{
			$where['_string']="find_in_set('".$this->login_id."',uids) or (is_all=1)";
		}
		//标题下拉菜单
		$db_notice=M('db_notice');
		if($type!=-10){$where['type']=$type;}
		$titles=$db_notice->where($where)->field('id,title')->select();
		$this->assign('titles',$titles);
		if($tid!=-10){$where['id']=$tid;}
		$count=$db_notice->where($where)->count();
		$Page=new \Think\Page($count,C('PAGE_LIST_ROWS')); 
		$show=$Page->show();
		$result=$db_notice->where($where)->order('add_date desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$db_notice_reply=M('db_notice_reply');
		//第一条数据
		$first_result=$db_notice->where($where)->order('add_date desc')->find();
		foreach($result as $key=>$value)
		{
			if($first_result['id']==$value['id']){
				$result[$key]['first']=1;
			}
			$result[$key]['add_user']=$this->get_username($value['add_uid']);
			//评论数量
			$where_ask['main_id']=$value['id'];
			$where_ask['reply_id']=0;
			$result[$key]['ask_count']=$db_notice_reply->where($where_ask)->count();
			//回复数量
			$where_ask['reply_id']=array('neq',0);
			$result[$key]['answer_count']=$db_notice_reply->where($where_ask)->count();
		}
		$this->assign('page',$show);
		$this->assign('result',$result);
		$this->display();
	}
	
	//公告-->添加 designed by:zhangsen 2017-08-30
	function notice_list_add(){
		parent::check_permission('GG');
		//选择用户下拉菜单
		$this->assign("user_list",$this->get_all_department());
		//添加
		if($_REQUEST['action']=="add")
		{
			$db_notice=M('db_notice'); 
			$where['title']=$_REQUEST['title'];
			$where['type']=$_REQUEST['type'];
			$where['is_all']=$_REQUEST['is_all'];
			if($_REQUEST['is_all']==2)
			{
				$where['uids']=$_REQUEST['uids'];
				$where['users']=$_REQUEST['users'];
			}
			if($_REQUEST['file_name']){$where['attachment_name']=$_REQUEST['file_name'];}
			if($_REQUEST['file_url']){$where['attachment']=$_REQUEST['file_url'];}
			$where['content']=$_REQUEST['content'];
			$where['add_uid']=$this->login_id;
			$where['add_user']=$this->login_username;
			$where['add_date']=date('Y-m-d H:i:s');
			$bool=$db_notice->add($where);
			$this->success("操作成功",__APP__."/System/notice_list");die;
		}
		$this->display();
	}

	//公告-->编辑 designed by:zhangsen 2017-08-30
	function notice_list_edit(){
		parent::check_permission('GG');
		//选择用户下拉菜单
		$this->assign("user_list",$this->get_all_department());
		//详情
		$db_notice=M('db_notice');
		$where_r['id']=$_REQUEST['id'];
		$result=$db_notice->where($where_r)->find();
		$this->assign('result',$result);
		//传值
		$this->assign('tid',$_REQUEST['tid']);
		$this->assign('type',$_REQUEST['type']);
		//编辑
		if($_REQUEST['action']=="edit")
		{
			//附件
			if($_REQUEST['file_url']){
				//判断是否修改过文件
				if($_REQUEST['file_url']!=$result['attachment']){
					$where['attachment']=$_REQUEST['file_url'];
					//删除原文件
					$file1=str_replace("/dms_erp/uploads", "./uploads",$result['attachment']);
					if (file_exists($file1)){
						@unlink($file1);
					}
				}
			}else{
				$where['attachment']=NULL;
				if($result['attachment']){
					//删除原文件
					$file1=str_replace("/dms_erp/uploads", "./uploads",$result['attachment']);
					if (file_exists($file1)){
						@unlink($file1);
					}
				}
			}
			if(!empty($_REQUEST['file_name'])){$where['attachment_name']=$_REQUEST['file_name'];}else{$where['attachment_name']=NULL;}
			//编辑
			$where['id']=$_REQUEST['id'];
			$where['title']=$_REQUEST['title'];
			$where['type']=$_REQUEST['type'];
			$where['is_all']=$_REQUEST['is_all'];
			if($_REQUEST['is_all']==2){
				$where['uids']=$_REQUEST['uids'];
				$where['users']=$_REQUEST['users'];
			}
			else{
				$where['uids']=NULL;
				$where['users']=NULL;
			}
			$where['content']=$_REQUEST['content'];
			$db_notice->save($where);
			$this->success("操作成功",__APP__."/System/notice_list/tid/".$_REQUEST['l_tid']."/type/".$_REQUEST['l_type']."/&p=".$_SESSION['page']);die;
		}
		$this->display();
	}

	//公告-->详情 designed by:zhangsen 2017-08-30
	function notice_list_details(){
		//奇创制度详情
		$db_notice=M('db_notice');
		$where_r['id']=$_REQUEST['id'];
		$result=$db_notice->where($where_r)->find();
		$result['add_user']=$this->get_username($result['add_uid']);
		$this->assign('result',$result);
		//显示评论与回复
		$db_notice_reply=M('db_notice_reply');
		$where_ask['main_id']=$_REQUEST['id'];
		$where_ask['reply_id']=0;
		$reply=$db_notice_reply->where($where_ask)->order('add_date')->select();
		foreach($reply as $key=>$value)
		{
			$reply[$key]['add_user']=$this->get_username($value['add_uid']);
			//回复
			$where_reply['reply_id']=$value['id'];
			$reply[$key]['answer']=$db_notice_reply->where($where_reply)->find();
			if(!empty($reply[$key]['answer'])){$reply[$key]['answer']['add_user']=$this->get_username($reply[$key]['answer']['add_uid']);}
			//能否回复
			if(empty($reply[$key]['answer']) and is_permission('GG'))
			{
				$reply[$key]['reply_able']=1;
			}
			//回复能否删除
			if(is_permission('GG') and (!empty($reply[$key]['answer']))) 
			{
				$reply[$key]['answer']['delete']=1;
			}
			//评论能否删除
			if(is_permission('GG') or (($value['add_uid']==$this->login_id) and (empty($reply[$key]['answer']))))
			{
				$reply[$key]['delete']=1;
			}
		}
		$this->assign('reply',$reply);
		//传值
		$this->assign('tid',$_REQUEST['tid']);
		$this->assign('confirmed',$_REQUEST['confirmed']);
		$this->assign('page',$_SESSION['page']);
		$this->display();
	}

	//公告-->评论/删除评论/回复/删除回复 designed by:zhangsen 2017-08-30
	function notice_list_details_ask(){
		$db_notice_reply=M('db_notice_reply');
		if($_REQUEST['action']=='comment')
		{
			//评论
			$where_add['main_id']=$_REQUEST['main_id'];
			$where_add['reply_id']=0;
			$where_add['content']=$_REQUEST['content'];
			$where_add['add_uid']=$this->login_id;
			$where_add['add_user']=$this->login_username;
			$where_add['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_notice_reply->add($where_add);
		}	
		elseif($_REQUEST['action']=='delete_comment')
		{
			//回复
			$where_replys['reply_id']=$_REQUEST['id'];
			$replys=$db_notice_reply->where($where_replys)->select();
			if(!empty($replys))
			{
				//删除回复
				foreach($replys as $key=>$value)
				{
					$where_delete_reply['id']=$value['id'];
					$db_notice_reply->where($where_delete_reply)->delete();
				}
			}
			//删除评论
			$where_delete_comment['id']=$_REQUEST['id'];
			$result_bool=$db_notice_reply->where($where_delete_comment)->delete();
		}
		elseif($_REQUEST['action']=='reply')
		{	
			//回复
			$where_add['main_id']=$_REQUEST['main_id'];
			$where_add['reply_id']=$_REQUEST['reply_id'];
			$where_add['content']=$_REQUEST['content'];
			$where_add['add_uid']=$this->login_id;
			$where_add['add_user']=$this->login_username;
			$where_add['add_date']=date('Y-m-d H:i:s');
			$result_bool=$db_notice_reply->add($where_add);
		}
		elseif($_REQUEST['action']=='delete_reply')
		{	
			//删除回复
			$where_delete['id']=$_REQUEST['id'];
			$result_bool=$db_notice_reply->where($where_delete)->delete();
		}
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}

	//公告-->删除 designed by:zhangsen 2017-08-30
	function notice_list_delete(){
		//详情
		$db_notice=M('db_notice');
		$where['id']=$_REQUEST['id'];
		$result=$db_notice->where($where)->find();
		//删除附件
		$url=$result['attachment'];
		$file=str_replace("/dms_erp/uploads", "./uploads",$url);
		if (file_exists($file)) {
			@unlink($file);
		}
		$result_bool=$db_notice->where($where)->delete();
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}
	
	//用户列表 designed by:zhangsen 2017-08-09
	function user_list(){
		parent::check_permission('YH');
		//赋值
		if(!empty($_REQUEST['keyword'])){$keyword=$_REQUEST['keyword'];}
		$this->assign('keyword',$keyword);
		if(!empty($_REQUEST['center_id'])){$center_id=$_REQUEST['center_id'];}else{$center_id=-10;}
		$this->assign('center_id',$center_id);
		if(!empty($_REQUEST['username'])){$username=$_REQUEST['username'];}else{$username=-10;}
		$this->assign('username',$username);
		if(!empty($_REQUEST['position_id'])){$position_id=$_REQUEST['position_id'];}else{$position_id=-10;}
		$this->assign('position_id',$position_id);
		if(!empty($_REQUEST['rank_id'])){$rank_id=$_REQUEST['rank_id'];}else{$rank_id=-10;}
		$this->assign('rank_id',$rank_id);
		if(!empty($_REQUEST['permissions_id'])){$permissions_id=$_REQUEST['permissions_id'];}else{$permissions_id=-10;}
		$this->assign('permissions_id',$permissions_id);
		if(!empty($_REQUEST['user_number'])){$user_number=$_REQUEST['user_number'];}else{$user_number=-10;}
		$this->assign('user_number',$user_number);
		if(!empty($_REQUEST['auditor'])){$auditor=$_REQUEST['auditor'];}else{$auditor=-10;}
		$this->assign('auditor',$auditor);
		if(!empty($_REQUEST['status'])){$status=$_REQUEST['status'];}else{$status=1;}
		$this->assign('status',$status);
		//所属中心筛选
		$this->assign('center_list',$this->get_oa_dep_list());
		//姓名下拉菜单
		$this->assign('result1',$this->get_all_username($status,$center_id==-10?NULL:$center_id));
		//职位下拉菜单
		$this->assign('result6',$this->library_type_list(2));
		//权限组下拉菜单
		$this->assign('result8',$this->get_permissions());
		//审核人下拉菜单
		$db_relationship=M('db_relationship');
		$where_relationship['db_relationship.status']=1;
		$result10=$db_relationship->field('leader_uid,leader_name')->join('LEFT JOIN db_user ON db_user.id=db_relationship.leader_uid')->where($where_relationship)->order('spell')->select();
		$this->assign('result10',$result10);
		//列表
		$db_user=M('db_user');
		if(!empty($keyword))
		{
			$map['username']=array('like','%'.$keyword.'%');
			$map['mobilephone']=array('like','%'.$keyword.'%');
			$map['user_number']=array('like','%'.$keyword.'%');
			$map['empl_id']=array('like','%'.$keyword.'%');
			$map['_logic']='or';
			$where['_complex']=$map;
		}
		if($center_id!=-10){$where['center_id']=$center_id;}
		if($username!=-10){$where['username']=$username;}else{$where['username']=array('neq','admin');}
		if($position_id!=-10){$where['position_id']=$position_id;}
		if($rank_id!=-10){$where['rank_id']=$rank_id;}
		if($permissions_id!=-10){$where['_string']='find_in_set("'.$permissions_id.'",permissions_id)';}
		if($user_number!=-10){$where['user_number']=$user_number;}
		if($auditor!=-10)
		{
			$where['id']=array('in',$this->user_list_ajax_relationship(4,$auditor,1));
		}
		$where['status']=$status;
		$count=$db_user->where($where)->count();
		$Page = new \Think\Page($count,15);
		$show=$Page->show();
		$result=$db_user->where($where)->order('update_date desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		//用户详情
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$db_permissions=M('db_permissions');
		foreach($result as $key=>$value)
		{
			$name_a_str=NULL;
			$where_a['id']=array('in',$value['permissions_id']);
			$name_a=$db_permissions->where($where_a)->select();
			foreach($name_a as $kk=>$vv)
			{
				$name_a_str.=$vv['permissions_name'].',';
			}
			$result[$key]['permissions_name']=rtrim($name_a_str,',');
			$department=$this->get_oa_dep_info($value['department_id']);
			$result[$key]['deps']=$department['HPS_DEPT_FULLDESCR'];
			$result[$key]['descr']=$department['DEPT_DESCR'];
			//审核人
			$relationship=$this->user_list_ajax_relationship(1,$value['id']);
			$result[$key]['audit_user']=$relationship['audit_user'];
		}
		$this->assign('page',$show);
		$this->assign('result',$result);
		//职位下拉
		$db_user=M('db_user');
		$where_4['position_descr']=array('exp','IS NOT NULL');
		$result_position=$db_user->field('position_id,position_descr')->where($where_4)->group('position_id')->order('position_id')->select();
		$this->assign('result_position',$result_position);
		//职级下拉
		$db_user=M('db_user');
		$where_5['rank_descr']=array('exp','IS NOT NULL');
		$result_rank=$db_user->field('rank_id,rank_descr')->where($where_5)->group('rank_id')->order('rank_id')->select();
		$this->assign('result_rank',$result_rank);
		$this->display();
	}

	//用户列表-->编辑 designed by:zhangsen 2017-08-09
	function user_list_edit(){
		parent::check_permission('YH');
		//用户详情
		$user=$this->get_user($_REQUEST['id']);
		$db_permissions=M('db_permissions');
		$where_p['id']=array('in',$user['permissions_id']);
		$result_p=$db_permissions->where($where_p)->select();
		foreach($result_p as $key=>$value){
			$user_str.=$value['permissions_name'].',';
		}
		$user['permissions_name']=rtrim($user_str,',');
		$center=$this->get_oa_dep_info($user['center_id']);
		$user['center']=$center['DEPT_DESCR'];
		$department=$this->get_oa_dep_info($user['department_id']);
		$user['deps']=$department['HPS_DEPT_FULLDESCR'];
		$this->assign('result',$user);
		//传值
		$this->assign('center_id',$_REQUEST['center_id']);
		$this->assign('username',$_REQUEST['username']);
		$this->assign('rank_id',$_REQUEST['rank_id']);
		$this->assign('position_id',$_REQUEST['position_id']);
		$this->assign('permissions_id',$_REQUEST['permissions_id']);
		$this->assign('auditor',$_REQUEST['auditor']);
		$this->assign('status',$_REQUEST['status']);
		$this->assign('page',$_SESSION['page']);
		//查出此用户的审核人/审批人
		$result7=$this->user_list_ajax_relationship(1,$_REQUEST['id']);
		$this->assign('result7',$result7);
		//判断姓名是否重复
		$db_user=M('db_user');
		$where_r['username']=$user['username'];
		$users=$db_user->where($where_r)->select();
		if(count($users)>1){
			$this->assign('repeat',1);
		}
		//编辑用户
		if($_REQUEST['action']=='edit')
		{
			$db_user=M('db_user');
			$user=$this->get_user($_REQUEST['id']);
			if(!empty($_REQUEST['regname'])){
				//判断登录名是否重复
				$where_regname['regname']=$_REQUEST['regname'];
				$regname_list=$db_user->where($where_regname)->find();
				if(!empty($regname_list) && ($user['regname']!=$_REQUEST['regname']))
				{
					$this->error("该登录用户名已存在！");die;
				}
				$where_edit['regname']=$_REQUEST['regname'];
			}
			//判断重名区分备注是否重复
			if(!empty($_REQUEST['repeat_name'])){
				$where_re['repeat_name']=$_REQUEST['repeat_name'];
				$repeat_name_list=$db_user->where($where_re)->find();
				if(!empty($repeat_name_list) && ($user['repeat_name']!=$_REQUEST['repeat_name']))
				{
					$this->error("该重名区分备注已存在！");die;
				}
			}
			if(!empty($_REQUEST['is_project'])){$where_edit['repeat_name']=$_REQUEST['repeat_name'];}else{$where_edit['is_project']=NULL;}
			$where_edit['id']=$_REQUEST['id'];
			$where_edit['password']=$_REQUEST['password'];
			$where_edit['permissions_id']=$_REQUEST['permissions_id'];
			if(!empty($_REQUEST['is_project'])){$where_edit['is_project']=$_REQUEST['is_project'];}else{$where_edit['is_project']=NULL;}
			$db_user->save($where_edit);
			//修改审核人
			if(($_REQUEST['auditor_edit']==-10) and empty($result7['audit_uid']))
			{
			}
			else
			{
				if($_REQUEST['auditor_edit']!=$result7['audit_uid'])
				{
					if(!empty($result7['audit_uid']))
					{
						$this->user_list_ajax_relationship(3,$_REQUEST['id'],1,$result7['audit_uid']);
					}
					if($_REQUEST['auditor_edit']!=-10)
					{
						$this->user_list_ajax_relationship(2,$_REQUEST['id'],1,$_REQUEST['auditor_edit']);
					}
				}
			}
			$this->success("操作成功",__APP__."/System/user_list/center_id/".$_REQUEST['center_id_s']."/username/".$_REQUEST['username_s']."/department_id/".$_REQUEST['department_id_s']."/position_id/".$_REQUEST['position_id_s']."/permissions_id/".$_REQUEST['permissions_id_s']."/auditor/".$_REQUEST['auditor_s']."/status/".$_REQUEST['status_s']."&p=".$_SESSION['page']);die;
		}
		//权限组
		$db_permissions=M('db_permissions');
		$permissions=$db_permissions->group('type')->order('type')->select();
		foreach($permissions as $key=>$value)
		{
			
			$type_list=$this->get_db_universal_type(14,1,NULL,NULL,$value['type']);
			$permissions[$key]['name']=$type_list['name2'];
			$where_user['type']=$value['type'];
			$where_user['permissions_name']=array('neq','超级管理员');
			$permissions[$key]['permissions']=$db_permissions->where($where_user)->select();
		}
		$this->assign('permissions',$permissions);
		$this->assign('user_list',$this->get_all_username());
		$this->display();
	}

	//用户列表-->禁用/启用 designed by:zhangsen 2017-08-09
	function user_list_ajax_status(){
		parent::check_permission('YH');
		$db_user=M('db_user');
		//用户详情
		$users=$this->get_user($_REQUEST['id']);
		if($_REQUEST['action']=='off')
		{
			//禁用
			$where['id']=$_REQUEST['id'];
			$where['status']=-1;
			$where['disable_date']=date('Y-m-d H:i:s');
			$result_bool=$db_user->save($where);
			//项目人员管理修改派出日期
			if($users['is_project']==1){
				$db_project_user=M('db_project_user');
				$where_p['uid']=$_REQUEST['id'];
				$where_p['out_date']=array('exp','IS NULL');
				$where_s['out_date']=date('Y-m-d');
				$db_project_user->where($where_p)->save($where_s);
			}
		}
		elseif($_REQUEST['action']=='on')
		{
			//启用
			$where['id']=$_REQUEST['id'];
			$where['status']=1;
			$where['disable_date']=NULL;
			$result_bool=$db_user->save($where);
			//项目人员管理添加待分配记录
			if($users['is_project']==1){
				$db_project_user=M('db_project_user');
				$where_p['uid']=$_REQUEST['id'];
				$where_p['pid']=NULL;
				$where_p['position']=$this->get_db_universal_label(2,$users['position_id']);
				$where_p['rank']=$this->get_db_universal_label(2,$users['rank_id']);
				$where_p['nature']='临时';
				$where_p['come_date']=date("Y-m-d");
				$where_p['out_date']=NULL;
				$where_p['add_uid']=$this->login_id;
				$where_p['add_date']=date("Y-m-d H:i:s");
				$db_project_user->add($where_p);
			}
		}
		if($result_bool)
		{
			$data['status']=self::YES;
		}
		else
		{
			$data['status']=self::NO;
		}
		echo json_encode($data);die;
	}
	
	//系统设置-->权限组 designed by:zhangsen 2017-08-09
	function permissions_group(){
		parent::check_permission('QX');
		//分类
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		//列表
		$db_permissions=M('db_permissions');
		if($this->login_username!='admin'){
			$where['permissions_name']=array('neq','超级管理员');
		}
		if($type!=-10){$where['type']=$type;}
		$result=$db_permissions->where($where)->order('id')->select();
		//是否可以删除权限
		$db_user=M('db_user');
		foreach($result as $key=>$value){
			$where_u['status']=1;
			$where_u['_string']="find_in_set('".$value['id']."',permissions_id)";
			$result_u=$db_user->where($where_u)->find();
			if(empty($result_u)){
				$result[$key]['is_del']=1;
			}
		}
		$this->assign('type_list',$this->get_db_universal_type(14));
		$this->assign('result',$result);
		$this->assign('sum',count($result));
		$this->display();
	}

	//系统设置-->权限组-->添加 designed by:zhangsen 2017-08-09
	function permissions_group_add(){
		parent::check_permission('QX');
		$db_permissions_list=M('db_permissions_list');
		$where['name_num']=array('neq','XT');
		$result=$db_permissions_list->where($where)->order('sort')->select();
		$this->assign('result',$result);
		if($_REQUEST['action']=="add")
		{
			$permissions_rule=$_REQUEST['permissions_rule'];
			$permissions_select=$_REQUEST['permissions_select'];
			$permissions_manage=$_REQUEST['permissions_manage'];
			$db_permissions=M('db_permissions');
			$where_zu['type']=$_REQUEST['type'];
			$where_zu['permissions_name']=$_REQUEST['permissions_name'];
			$where_zu['remark']=$_REQUEST['remark']?$_REQUEST['remark']:NULL;
			$judge=$db_permissions->field('id')->where($where_zu)->find();
			if(empty($judge)){
				$where_zu['select_id']=NULL;
				$where_zu['manage_id']=NULL;
				foreach($permissions_rule as $key=>$value){
					$str_rule.=$value.'_'.$permissions_select[$key].'_'.$permissions_manage[$key].',';
					if($value=='XM'){
						$where_zu['select_id']=join(',',$_REQUEST['select_id'])?join(',',$_REQUEST['select_id']):NULL;
						$where_zu['manage_id']=join(',',$_REQUEST['manage_id'])?join(',',$_REQUEST['manage_id']):NULL;
					}
					if($value=='SW'){
						$where_zu['sw_select_id']=join(',',$_REQUEST['sw_select_id'])?join(',',$_REQUEST['sw_select_id']):NULL;
						$where_zu['sw_manage_id']=join(',',$_REQUEST['sw_manage_id'])?join(',',$_REQUEST['sw_manage_id']):NULL;
					}
				}
				$where_zu['permissions_rule']=rtrim($str_rule,',');
				$db_permissions->add($where_zu);
				$this->success('操作成功',__APP__."/System/permissions_group");die;
			}else{
				$this->error('权限组名称重复，请重新添加！',__APP__."/System/permissions_group_add",3);die;
			}
		}
		$type_list=$this->get_db_universal_type(14);
		foreach($type_list as $kk=>$vv){
			if($vv['name1']==-10){unset($type_list[$kk]);}
		}
		$this->assign('type_list',$type_list);
		$this->display();
	}

	//系统设置-->权限组-->编辑 designed by:zhangsen 2017-08-09
	function permissions_group_edit(){
		parent::check_permission('QX');
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=-10;}
		$this->assign('type',$type);
		$db_permissions=M('db_permissions');
		$where_zu['id']=$_REQUEST['id'];
		$result_zu=$db_permissions->where($where_zu)->find();
		$db_permissions_list=M('db_permissions_list');
		$where['name_num']=array('neq','XT');
		$result=$db_permissions_list->where($where)->order('sort')->select();
		$permissions_rule=explode(',',$result_zu['permissions_rule']);
		foreach($result as $key=>$value)
		{
			foreach($permissions_rule as $k=>$v){
				if(preg_match('/'.$value['name_num'].'/',$v)){
					$result[$key]['num1']=$value['name_num'];
					if(preg_match('/CRM/',$v)){
						$result[$key]['num2']=substr($v,7,2);
						$result[$key]['num3']=substr($v,10,2);
					}else{
						$result[$key]['num2']=substr($v,3,2);
						$result[$key]['num3']=substr($v,6,2);
					}
				}	
			}
		}
		$this->assign('result',$result);
		$this->assign('id',$_REQUEST['id']);
		$this->assign('permissions_name',$result_zu['permissions_name']);
		$this->assign('result_zu',$result_zu);
		if($_REQUEST['action']=="edit")
		{
			$permissions_rule=$_REQUEST['permissions_rule'];
			$permissions_select=$_REQUEST['permissions_select'];
			$permissions_manage=$_REQUEST['permissions_manage'];
			$where_s['permissions_name']=$_REQUEST['permissions_name'];
			$where_s['id']=array('neq',$_REQUEST['id']);
			$judge=$db_permissions->field('id')->where($where_s)->find();
			if(empty($judge)){
				$where_save['select_id']=NULL;
				$where_save['manage_id']=NULL;
				foreach($permissions_rule as $key=>$value){
					$str_rule.=$value.'_'.$permissions_select[$key].'_'.$permissions_manage[$key].',';
					if($value=='XM'){
						$where_save['select_id']=join(',',$_REQUEST['select_id'])?join(',',$_REQUEST['select_id']):NULL;
						$where_save['manage_id']=join(',',$_REQUEST['manage_id'])?join(',',$_REQUEST['manage_id']):NULL;
					}
					if($value=='SW'){
						$where_save['sw_select_id']=join(',',$_REQUEST['sw_select_id'])?join(',',$_REQUEST['sw_select_id']):NULL;
						$where_save['sw_manage_id']=join(',',$_REQUEST['sw_manage_id'])?join(',',$_REQUEST['sw_manage_id']):NULL;
					}
				}
				$where_save['id']=$_REQUEST['id'];
				$where_save['type']=$_REQUEST['type'];
				$where_save['permissions_name']=$_REQUEST['permissions_name'];
				$where_save['remark']=$_REQUEST['remark']?$_REQUEST['remark']:NULL;
				if($_REQUEST['permissions_name']!='超级管理员'){
					$where_save['permissions_rule']=rtrim($str_rule,',');
				}else{
					$where_save['permissions_rule']='XT_U1_Q1,'.rtrim($str_rule,',');
				}
				$db_permissions->save($where_save);
				$this->success('操作成功',__APP__."/System/permissions_group/type/".$_REQUEST['ptype']);die;
			}else{
				$this->error('权限组名称重复，请重新添加！',__APP__."/System/permissions_group_edit/id/".$_REQUEST['id'],3);die;
			}
		}
		$type_list=$this->get_db_universal_type(14);
		foreach($type_list as $kk=>$vv){
			if($vv['name1']==-10){unset($type_list[$kk]);}
		}
		$this->assign('type_list',$type_list);
		$this->display();
	}

	//系统设置-->权限组-->删除 designed by:zhangsen 2017-08-09
	function permissions_group_delete(){
		parent::check_permission('QX');
		$db_permissions=M('db_permissions');
		$where['id']=$_REQUEST['id'];
		$data['status']=$db_permissions->where($where)->delete();
		$data['status']=self::YES;
		echo json_encode($data);die;
	}
	
	//系统设置-->审核流程管理 designed by:zhaoshihao 2017-03-02
	function audit_schedule_manage(){
		//赋值
		if(!empty($_REQUEST['type'])){$type=$_REQUEST['type'];}else{$type=1;}
		$this->assign('type',$type);
		//全部人员下拉菜单
		$this->assign('user_list',$this->get_all_department());
		//审核功能
		$this->assign('schedule_types',$this->get_db_universal_type(7));
		//获取审核流程
		$this->assign('audit_process',$this->get_audit_process($type,1));
		$this->assign('audit_process_count',count($this->get_audit_process($type,1)));
		//列表
		$db_universal_audit=M('db_universal_audit');
		$where['type']=$type;
		if($type==6){
			$center_types[0]['first_name']='全部';
			$center_types[0]['process']=$db_universal_audit->where($where)->order('audit_sort')->select();
			$center_types[0]['count']=count($center_types[0]['process']);
		}elseif($type==7){
			$center_types[0]['first_name']='全部';
			$center_types[0]['process']=$db_universal_audit->where($where)->order('audit_sort')->select();
			$center_types[0]['count']=count($center_types[0]['process']);
		}else{
			//审核中心
			$center_types=$this->get_oa_dep_list();
			foreach($center_types as $key=>$value)
			{
				if(!empty($value['id'])){$where['audit_type']=$value['id'];}
				$center_types[$key]['process']=$db_universal_audit->where($where)->order('audit_sort')->select();
				$center_types[$key]['count']=count($center_types[$key]['process']);
			}
		}
		$this->assign('center_types',$center_types);
		$this->display();
	}

	//系统设置-->审核流程管理-->ajax操作 designed by:zhaoshihao 2017-03-02
	function audit_schedule_manage_ajax(){
		$db_universal_audit=M('db_universal_audit');
		if($_REQUEST['action']=='edit_user')
		{
			//修改人员
			$where_find['id']=$_REQUEST['mid'];
			$result=$db_universal_audit->where($where_find)->find();
			if(empty($result['audit_uid']))
			{
				//修改进行中单据的状态
				$where_max['type']=$result['type'];
				$where_max['audit_type']=$result['audit_type'];
				$where_max['audit_uid']=array('exp','is not NULL');
				$where_max['audit_user']=array('exp','is not NULL');
				$sort_max=$db_universal_audit->where($where_max)->max('audit_sort');
				if($result['audit_sort']<$sort_max)
				{
					$next_sort=$this->audit_type_next($result['type'],$result['audit_type'],$result['audit_sort'],'gt');
					$table=$this->get_db_universal_type(7,1,NULL,NULL,NULL,$result['type']);
					$model=M($table['name3']);
					$where_on['audit_type']=$result['audit_type'];
					$where_on['status']=$next_sort['audit_sort'];
					$danju=$model->field('id')->where($where_on)->select();
					if(!empty($danju))
					{
						foreach($danju as $key=>$value)
						{
							$where_save['id']=$value['id'];
							$where_save['status']=$result['audit_sort'];
							$model->save($where_save);
						}
					}
				}
			}
			$where_edit['id']=$_REQUEST['mid'];
			$where_edit['audit_uid']=$_REQUEST['uid'];
			$where_edit['audit_user']=$this->get_username($_REQUEST['uid']);
			$result_bool=$db_universal_audit->save($where_edit);
		}
		elseif($_REQUEST['action']=='clear')
		{
			//删除人员
			$where_find['id']=$_REQUEST['mid'];
			$result=$db_universal_audit->where($where_find)->find();
			$table=$this->get_db_universal_type(7,1,NULL,NULL,NULL,$result['type']);
			$model=M($table['name3']);
			$where_max['type']=$result['type'];
			$where_max['audit_type']=$result['audit_type'];
			$where_max['audit_uid']=array('exp','is not NULL');
			$where_max['audit_user']=array('exp','is not NULL');
			$where_max['audit_sort']=array('gt',$result['audit_sort']);
			$sort_max=$db_universal_audit->where($where_max)->find();
			if(!empty($sort_max))
			{
				//非最后流程
				$next_sort=$this->audit_type_next($result['type'],$result['audit_type'],$result['audit_sort'],'gt');
				$where_save['status']=$next_sort['audit_sort'];
			}
			else
			{
				//最后流程
				$where_last['type']=$result['type'];
				$where_last['audit_type']=$result['audit_type'];
				$where_save['status']=1+$db_universal_audit->where($where_last)->max('audit_sort');
			}
			$where_on['audit_type']=$result['audit_type'];
			$where_on['status']=$result['audit_sort'];
			$danju=$model->field('id')->where($where_on)->select();
			if(!empty($danju))
			{
				foreach($danju as $key=>$value)
				{
					$where_save['id']=$value['id'];
					$model->save($where_save);
				}
			}
			$where_clear['id']=$_REQUEST['mid'];
			$where_clear['audit_uid']=NULL;
			$where_clear['audit_user']=NULL;
			$result_bool=$db_universal_audit->save($where_clear);
		}
		if($result_bool)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}

	//关联功能管理 designed by:zhaoshihao 2017-07-20
	function label_administration_three(){
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$db_customers_information=M('db_customers_information');
		$db_user=M('db_user');
		$where_keyword['classif']=array('in',$_REQUEST['classif']);
		$all_keywords=$db_universal_label1->field('id,first_name')->where($where_keyword)->order('classif,first_sort')->select();
		foreach($all_keywords as $key=>$value){
			$where_second['first_id']=$value['id'];
			$all_keywords[$key]['second']=$db_universal_label2->field('id,second_name')->where($where_second)->order('second_sort')->select();
			$all_keywords[$key]['sum']=count($all_keywords[$key]['second']);
			$kk=0;
			foreach($all_keywords[$key]['second'] as $k1=>$v1){
				$where_third['first_id']=$value['id'];
				$where_third['second_id']=$v1['id'];
				$all_keywords[$key]['second'][$k1]['third']=$db_universal_label3->field('id,third_name,first_id')->where($where_third)->order('third_sort')->select();
				$all_keywords[$key]['second'][$k1]['sum']=count($all_keywords[$key]['second'][$k1]['third']);
				foreach($all_keywords[$key]['second'][$k1]['third'] as $k3=>$v3){
					if($v3['first_id']==69){
						$where_d['_string']="find_in_set(".$v3['id'].",customer_type)";
						$has=$db_customers_information->where($where_d)->find();
						if(!empty($has)){$all_keywords[$key]['second'][$k1]['del']=1;$all_keywords[$key]['second'][$k1]['third'][$k3]['del']=1;}
					}elseif($v3['first_id']==68){
						$where_ds['channel']=$v3['id'];
						$has=$db_customers_information->where($where_ds)->find();
						if(!empty($has)){$all_keywords[$key]['second'][$k1]['third'][$k3]['del']=1;}
					}
				}
			}
		}
		$this->assign('all_keywords',$all_keywords);
		$this->assign('classif',$_REQUEST['classif']);
		$this->assign('types',$_REQUEST['types']);
		$this->display();
	}
	
	//关联功能管理-->ajax designed by:zhaoshihao 2017-07-20
	function label_administration_three_ajax(){
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$key=explode(',',$_REQUEST['classif']);
		foreach($key as $kk=>$vv){
			if($vv==2){unset($key[$kk]);}
		}
		$where_keyword['classif']=array('in',$key);
		if($_REQUEST['labels']==1){
			if($_REQUEST['tj']==2){
				$where_keyword['classif']=17;
			}elseif($_REQUEST['tj']==3){
				$where_keyword['classif']=array('in','16,17');
			}
			$data=$db_universal_label1->field('id,first_name as name')->where($where_keyword)->order('first_sort')->select();
		}elseif($_REQUEST['labels']==2){
			$where_keyword['first_id']=$_REQUEST['fid'];
			$data=$db_universal_label2->field('id,second_name as name')->where($where_keyword)->order('second_sort')->select();
		}elseif($_REQUEST['labels']==3){
			$where_keyword['first_id']=$_REQUEST['fid'];
			$where_keyword['second_id']=$_REQUEST['sid'];
			$data=$db_universal_label3->field('id,third_name as name')->where($where_keyword)->order('third_sort')->select();
		}elseif($_REQUEST['labels']==4){
			if($_REQUEST['dele']==2){
				//获取该一级分类下的二级分类
				$where['id']=$_REQUEST['id'];
				$second=$db_universal_label2->where($where)->find();
				$where_keyword['first_id']=$second['first_id'];
				$where_keyword['id']=array('neq',$_REQUEST['id']);
				$where_keyword['second_name']=array('neq','全部');
				$data=$db_universal_label2->field('id,second_name as name')->where($where_keyword)->order('second_sort')->select();
			}else{
				//获取该二级分类下的三级分类
				$where['id']=$_REQUEST['id'];
				$third=$db_universal_label3->where($where)->find();
				$where_keyword['first_id']=$third['first_id'];
				$where_keyword['second_id']=$third['second_id'];
				$where_keyword['id']=array('neq',$_REQUEST['id']);
				$where_keyword['third_name']=array('neq','全部');
				$data=$db_universal_label3->field('id,third_name as name')->where($where_keyword)->order('third_sort')->select();
			}
			
		}
		echo json_encode($data);die;
	}
	
	//关联功能管理-->操作 designed by:zhaoshihao 2017-07-20
	function label_administration_three_operate(){
		$db_universal_label1=M('db_universal_label1');
		$db_universal_label2=M('db_universal_label2');
		$db_universal_label3=M('db_universal_label3');
		$db_user=M('db_user');
		if($_REQUEST['action']=='delete'){
			//删除标签
			if($_REQUEST['labels']==1){
				$where_t['first_id']=$_REQUEST['id'];
				$third=$db_universal_label3->where($where_t)->select();
				foreach($third as $key=>$value){
					$where['_string']='find_in_set("'.$value['id'].'",keyword_ids)';
					$result=$db_task_adv->where($where)->select();
					foreach($result as $kk=>$vv){
						$keyword_array=explode(',',$vv['keyword_ids']);
						foreach($keyword_array as $k1=>$v1){
							if($v1==$value['id']){unset($keyword_array[$k1]);$keyword_array[]=1;}
						}
						$keyword_array=array_unique($keyword_array);
						$where_a['id']=$vv['id'];
						$where_a['keyword_ids']=join(',',$keyword_array);
						$db_task_adv->save($where_a);
					}
					$where_d['id']=$value['id'];
					$db_universal_label3->where($where_d)->delete();
				}
				$where_s['first_id']=$_REQUEST['id'];
				$db_universal_label2->where($where_s)->delete();
				$where_f['id']=$_REQUEST['id'];
				$result_bool=$db_universal_label1->where($where_f)->delete();
				$arr=$db_universal_label1->where('classif=1')->order('first_sort')->select();
				foreach($arr as $key=>$value){
					$where_nt['id']=$value['id'];
					$where_nt['first_sort']=$key+1;
					$db_universal_label1->save($where_nt);
				}
			}elseif($_REQUEST['labels']==2){
				$where_t['second_id']=$_REQUEST['id'];
				$third=$db_universal_label3->where($where_t)->select();
				foreach($third as $key=>$value){
					$where_d['id']=$value['id'];
					$db_universal_label3->where($where_d)->delete();
				}
				$where_s['id']=$_REQUEST['id'];
				$result_bool=$db_universal_label2->where($where_s)->delete();
			}elseif($_REQUEST['labels']==3){
				$where_t['id']=$_REQUEST['id'];
				$dethird=$db_universal_label3->where($where_t)->find();
				/*
				if($_REQUEST['bool']!=1){
					$where_f['id']=$dethird['first_id'];
					$first=$db_universal_label1->where($where_f)->find();
					if($first['classif']==69){
						$where_a['_string']='find_in_set("'.$_REQUEST['id'].'",position_id)';
						$third=$db_user->where($where_a)->select();
						foreach($third as $key=>$value){
							$arr=explode(',',$value['position_id']);
							foreach($arr as $kk=>$vv){
								if($vv==$_REQUEST['id']){
									$arr[$kk]=$_REQUEST['cid'];
								}
							}
							$result=array_unique($arr);
							sort($result);
							$where_o['position_id']=join(",",$result);
							$where_o['id']=$value['id'];
							$db_user->save($where_o);
						}
					}elseif($first['classif']==68){
						$where_a['department_id']=$_REQUEST['id'];
						$third=$db_user->where($where_a)->select();
						foreach($third as $key=>$value){
							$where_o['department_id']=$_REQUEST['cid'];
							$where_o['id']=$value['id'];
							$db_user->save($where_o);
						}
					}
				}
				*/
				//删三级分类
				$result_bool=$db_universal_label3->where($where_t)->delete();
				//三级分类重新排序
				$where_sort['first_id']=$dethird['first_id'];
				$where_sort['second_id']=$dethird['second_id'];
				$arr=$db_universal_label3->where($where_sort)->order('third_sort')->select();
				foreach($arr as $key=>$value){
					$where_nt['id']=$value['id'];
					$where_nt['third_sort']=$key+1;
					$db_universal_label3->save($where_nt);
				}
			}
		}
		elseif($_REQUEST['action']=='add')
		{
			//添加标签
			if($_REQUEST['labels']==1){
				$where['classif']=1;
				$where['first_name']=$_REQUEST['first'];
				$first=$db_universal_label1->where($where)->find();
				if(empty($first)){
					$first_sort=$db_universal_label1->where('classif=1')->max('first_sort');
					$where['first_sort']=$first_sort+1;
					$where['add_uid']=$this->login_id;
					$where['add_user']=$this->login_username;
					$where['add_date']=date('Y-m-d H:i:s');
					$fid=$db_universal_label1->add($where);
					$where_s['first_id']=$fid;
					$where_s['second_name']=$_REQUEST['second'];
					$where_s['second_sort']=1;
					$where_s['add_uid']=$this->login_id;
					$where_s['add_user']=$this->login_username;
					$where_s['add_date']=date('Y-m-d H:i:s');
					$sid=$db_universal_label2->add($where_s);
					$where_t['first_id']=$fid;
					$where_t['second_id']=$sid;
					$where_t['third_name']=$_REQUEST['third'];
					$where_t['third_sort']=1;
					$where_t['add_uid']=$this->login_id;
					$where_t['add_user']=$this->login_username;
					$where_t['add_date']=date('Y-m-d H:i:s');
					$result_bool=$db_universal_label3->add($where_t);
				}else{
					$where_s['first_id']=$first['id'];
					$where_s['second_name']=$_REQUEST['second'];
					$second=$db_universal_label2->where($where_s)->find();
					if(empty($second)){
						$second_sort=$db_universal_label2->where('first_id='.$first['id'].'')->max('second_sort');
						$where_s['second_sort']=$second_sort+1;
						$where_s['add_uid']=$this->login_id;
						$where_s['add_user']=$this->login_username;
						$where_s['add_date']=date('Y-m-d H:i:s');
						$sid=$db_universal_label2->add($where_s);
						$where_t['first_id']=$first['id'];
						$where_t['second_id']=$sid;
						$where_t['third_name']=$_REQUEST['third'];
						$where_t['third_sort']=1;
						$where_t['add_uid']=$this->login_id;
						$where_t['add_user']=$this->login_username;
						$where_t['add_date']=date('Y-m-d H:i:s');
						$result_bool=$db_universal_label3->add($where_t);
					}else{
						$where_t['first_id']=$first['id'];
						$where_t['second_id']=$second['id'];
						$where_t['third_name']=$_REQUEST['third'];
						$third=$db_universal_label3->where($where_t)->find();
						if(empty($third)){
							$third_sort=$db_universal_label3->where('first_id='.$first['id'].' and second_id='.$second['id'].'')->max('third_sort');
							$where_t['third_sort']=$third_sort+1;
							$where_t['add_uid']=$this->login_id;
							$where_t['add_user']=$this->login_username;
							$where_t['add_date']=date('Y-m-d H:i:s');
							$result_bool=$db_universal_label3->add($where_t);
						}else{
							$data['status']=self::NO;
							$data['msg']='已有分类';
							echo json_encode($data);die;
						}
					}
				}
			}elseif($_REQUEST['labels']==2){
				$where_s['first_id']=$_REQUEST['first'];
				$where_s['second_name']=$_REQUEST['second'];
				$second=$db_universal_label2->where($where_s)->find();
				if(empty($second)){
					$where_t['first_id']=$_REQUEST['first'];
					$where_t['second_id']=$second['id'];
					$where_t['third_name']=$_REQUEST['third'];
					$third=$db_universal_label3->where($where_t)->find();
					if(empty($third)){
						$second_sort=$db_universal_label2->where('first_id='.$_REQUEST['first'].'')->max('second_sort');
						$where_s['second_sort']=$second_sort+1;
						$where_s['add_uid']=$this->login_id;
						$where_s['add_user']=$this->login_username;
						$where_s['add_date']=date('Y-m-d H:i:s');
						$sid=$db_universal_label2->add($where_s);
						$where_ts['first_id']=$_REQUEST['first'];
						$where_ts['second_id']=$sid;
						$where_ts['third_name']=$_REQUEST['third'];
						$where_ts['third_sort']=1;
						$where_ts['add_uid']=$this->login_id;
						$where_ts['add_user']=$this->login_username;
						$where_ts['add_date']=date('Y-m-d H:i:s');
						$result_bool=$db_universal_label3->add($where_ts);
					}else{
						$data['status']=self::NO;
						$data['msg']='已有三级分类';
						echo json_encode($data);die;
					}
				}else{
					$data['status']=self::NO;
					$data['msg']='已有二级分类';
					echo json_encode($data);die;
					
				}
			}elseif($_REQUEST['labels']==3){
				$where_t['first_id']=$_REQUEST['first'];
				$where_t['second_id']=$_REQUEST['second'];
				$where_t['third_name']=$_REQUEST['third'];
				$third=$db_universal_label3->where($where_t)->find();
				if(empty($third)){
					$third_sort=$db_universal_label3->where('first_id='.$_REQUEST['first'].' and second_id='.$_REQUEST['second'].'')->max('third_sort');
					$where_t['third_sort']=$third_sort+1;
					$where_t['add_uid']=$this->login_id;
					$where_t['add_user']=$this->login_username;
					$where_t['add_date']=date('Y-m-d H:i:s');
					$result_bool=$db_universal_label3->add($where_t);
				}else{
					$data['status']=self::NO;
					$data['msg']='已有三级分类';
					echo json_encode($data);die;
				}
			}
		}
		elseif($_REQUEST['action']=='edit')
		{
			//编辑标签
			if($_REQUEST['labels']==1){
				$where['classif']=1;
				$where['first_name']=$_REQUEST['name'];
				$first=$db_universal_label1->where($where)->find();
				if(empty($first)){
					$where['id']=$_REQUEST['id'];
					$result_bool=$db_universal_label1->save($where);
				}else{
					$data['status']=self::NO;
					$data['msg']='已有一级分类';
					echo json_encode($data);die;
				}
			}elseif($_REQUEST['labels']==2){
				$where['id']=$_REQUEST['id'];
				$name=$db_universal_label2->where($where)->find();
				$where_s['first_id']=$name['first_id'];
				$where_s['second_name']=$_REQUEST['name'];
				$second=$db_universal_label2->where($where_s)->find();
				if(empty($second)){
					$where['second_name']=$_REQUEST['name'];
					$result_bool=$db_universal_label2->save($where);
				}else{
					$data['status']=self::NO;
					$data['msg']='已有二级分类';
					echo json_encode($data);die;
				}
			}elseif($_REQUEST['labels']==3){
				$where['id']=$_REQUEST['id'];
				$name=$db_universal_label3->where($where)->find();
				$where_t['first_id']=$name['first_id'];
				$where_t['second_id']=$name['second_id'];
				$where_t['third_name']=$_REQUEST['name'];
				$third=$db_universal_label3->where($where_t)->find();
				if(empty($third)){
					$where['third_name']=$_REQUEST['name'];
					$result_bool=$db_universal_label3->save($where);
				}else{
					$data['status']=self::NO;
					$data['msg']='已有三级分类';
					echo json_encode($data);die;
				}
			}
		}
		elseif($_REQUEST['action']=='sort')
		{
			//标签排序
			if($_REQUEST['labels']==1){
				foreach($_REQUEST['mid_array'] as $key=>$value){
					$where['id']=$value;
					$where['first_sort']=$key+1;
					$db_universal_label1->save($where);
					$result_bool=1;
				}
			}elseif($_REQUEST['labels']==2){
				foreach($_REQUEST['mid_array'] as $key=>$value){
					$where['id']=$value;
					$where['second_sort']=$key+1;
					$db_universal_label2->save($where);
					$result_bool=1;
				}
			}elseif($_REQUEST['labels']==3){
				foreach($_REQUEST['mid_array'] as $key=>$value){
					$where['id']=$value;
					$where['third_sort']=$key+1;
					$db_universal_label3->save($where);
					$result_bool=1;
				}
			}
		}
		if($result_bool!==false)
		{
			$data['status']=self::YES;
			$data['msg']='操作成功';
		}
		else
		{
			$data['status']=self::NO;
			$data['msg']='操作失败';
		}
		echo json_encode($data);die;
	}
	
	//提醒 designed by:zhangsen 2017-12-13
	function message_list(){
		$db_alert=M('db_alert');
		//有id标记为已读
		if($_REQUEST['id']){
			$save_main['id']=$_REQUEST['id'];
			$save_main['status']=1;
			$db_alert->save($save_main);	
		}
		//状态
		if(!empty($_REQUEST['status'])){$status=$_REQUEST['status'];}else{$status=-10;}
		$this->assign('status',$status);
		//类型
		if(!empty($_REQUEST['alert_type'])){$alert_type=$_REQUEST['alert_type'];}else{$alert_type=1000;}
		$this->assign('alert_type',$alert_type);
		
		import("ORG.Util.Page");
		if($status!=-10){$where['status']=$status;}
		if($alert_type!=1000){$where['db_alert.type']=$alert_type;}
		$where['to_uid']=$this->login_id;
		$where['to_user']=$this->login_username;
		$count=$db_alert->field('db_alert.id,content,url,status,add_date,remark,to_uid,add_user,db_alert.type')->join('LEFT JOIN db_alert_type ON db_alert_type.id=db_alert.alert_type_id')->where($where)->count();
		$Page = new \Think\Page($count,C('PAGE_LIST_ROWS')); 
		$show=$Page->show();
		$alert_result=$db_alert->field('db_alert.id,content,url,status,add_date,remark,to_uid,add_user,db_alert.type')->join('LEFT JOIN db_alert_type ON db_alert_type.id=db_alert.alert_type_id')->where($where) ->order('add_date desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($alert_result as $k=>$v)
		{
			$url1=explode("href='",$v['remark']);
			$url2=explode("'><span",$url1[1]);
			$url3=explode("<a",$v['remark']);
			if(count($url1)==2)
			{
				$alert_result[$k]['remark']=$url3[0];
				$alert_result[$k]['url']=$url2[0];
			}
		}
		$this->assign('list',$alert_result);
		$this->assign('page',$show); 
		$this->assign('status',$status);
		//未读提醒数量
		$al_ct=$this->get_alert_num();
		$this->assign('al_ct',$al_ct['al_ct']?$al_ct['al_ct']:NULL);
		$this->display();
	}

	//提醒-->列表-->已读 designed by:zhangsen 2017-12-13
	function message_list_read(){
		isset($_REQUEST['id'])?$id=$_REQUEST['id']:$id=0;
		$db_alert=M('db_alert');
		$where['id']=$id;
		$where['status']=1;
		echo $db_alert->save($where);
	}

	//提醒-->列表-->批量已读 designed by:zhangsen 2017-12-13
	function message_list_reads(){
		foreach($_REQUEST['id'] as $key=>$value)
		{
			$db_alert=M('db_alert');
			$where['id']=$value;
			$where['status']=1;
			$db_alert->save($where);
		}
		$this -> success('操作成功!',__APP__."/System/message_list/status/".$_REQUEST['status']."/alert_type/".$_REQUEST['alert_type']."/?&p=".$_SESSION['page']);die;
	}

	//提醒-->列表-->点此跳转已读 designed by:zhangsen 2017-12-13
	function message_list_tz_read(){
		$db_alert=M('db_alert');
		$where['id']=$_REQUEST['id'];
		$where['status']=1;
		$db_alert->save($where);
	}

	//提醒-->列表-->全部已读 designed by:zhangsen 2017-12-13
	function message_list_all_read(){
		$db_alert=M('db_alert');
		$where_t['_string']='find_in_set("'.$this->login_id.'",to_uid)';
		$where['status']=1;
		$db_alert->where($where_t)->save($where);
		$this -> success('操作成功!',__APP__."/System/message_list/status/".$_REQUEST['status']);die;
	}
	
	//清除缓存 designed by:zhangsen 2017-08-09
	function c_c(){
		parent::check_permission('XT');
		exec('rm -rf /home/dms_erp/ERP/Runtime/*');
		echo "清除缓存成功!";
		$this->redirect("Index/index");
	}

	//退出 designed by:zhangsen 2017-08-09
	function login_out(){
		session_destroy();
        setcookie("goto","");
		$this->redirect('Index/login');
	}
}
?>